package view;

import java.util.Map;

import javax.swing.JEditorPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.DebateFormat;
import model.MainModel;
import algo.DistributionResult;

import com.google.inject.Inject;
import com.google.inject.Provider;

public class ResultPane extends JEditorPane implements ChangeListener {

	private final MainModel model;
	private final Map<DebateFormat, Provider<ResultFormatter>> formatters;

	@Inject
	public ResultPane(MainModel model, Map<DebateFormat, Provider<ResultFormatter>> formatters) {
		super();
		this.model = model;
		this.formatters = formatters;
		model.addChangeListener(this);
		setContentType("text/html");
	}

	public void clearResult() {
		setText("");
	}

	@Override
	public void stateChanged(ChangeEvent arg0) {
		// Update result
		DistributionResult result = model.getResult();
		if (result == null) {
			return;
		}
		
		StringBuilder text = new StringBuilder("<html><head></head><body>");
		for (DistributionResult.SingleDebateDistribution dist : result
				.getDistributions()) {
			ResultFormatter formatter = formatters.get(dist.getFormat()).get();
			text.append(formatter.getFormattedResult(dist));
		}
		text.append("</body></html>");
		setText(text.toString());
	}
}
